package com.sky.robot.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.sky.robot.consts.SqlCode;
import com.sky.robot.entity.SkyTable;
import com.sky.robot.entity.SkyTableColumn;
import org.apache.commons.lang3.text.WordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
public class SystemUtils {

    // 静态 JdbcTemplate 变量
    private static JdbcTemplate staticJdbcTemplate;

    // 普通 JdbcTemplate 注入
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 使用 @PostConstruct 将实例变量赋值给静态变量
    @PostConstruct
    public void init() {
        staticJdbcTemplate = this.jdbcTemplate;
    }

    public static Boolean createTable(SkyTable table, List<SkyTableColumn> tableColumns) {

        String sqlType = PropertiesUtils.getString("sky.sql.type");

        if (DbType.MYSQL.getDb().equals(sqlType)) {
            createMySql(table, tableColumns);
        }


        return true;
    }

    /**
     * 创建MySQL语句
     */
    public static void createMySql(SkyTable table, List<SkyTableColumn> tableColumns) {
        StringBuilder colSql = new StringBuilder();

        boolean existsTable = Boolean.parseBoolean(PropertiesUtils.getString("sky.sql.drop.table.if.exists"));
        if (existsTable) {
            colSql.append("DROP TABLE IF EXISTS ").append(table.getTableName()).append(";");
        }

        colSql.append(" CREATE TABLE `").append(table.getTableName()).append("` (");

        for (SkyTableColumn tableColumn : tableColumns) {
            String colName = tableColumn.getColCode(); // 字段名称
            String colType = tableColumn.getColType(); // 字段类型
            Integer colLength = tableColumn.getColLength(); // 字段长度
            String colAnnotation = tableColumn.getColAnnotation(); // 注释
            String defaultValue = tableColumn.getDefaultValue(); // 默认值
            colSql.append(colName).append(" ").append(colType).append(changeLength(colType, String.valueOf(colLength)))
                    .append(" DEFAULT ").append(changeDefaultValue(colType, defaultValue)).append(" ")
                    .append(" COMMENT '").append(colAnnotation).append("', ");
        }

        colSql.append(" PRIMARY KEY (`id`) ) ENGINE=").append(table.getDatabaseEngine())
                .append(" DEFAULT CHARSET=").append(table.getCharType());

        System.out.println(colSql);

        staticJdbcTemplate.execute(String.valueOf(colSql));
    }


    /**
     * 列名转换成Java属性名
     */
    public static String columnToJava(String columnName) {
        return WordUtils.capitalizeFully(columnName, '_').replace("_", "");
    }


    private static String changeDefaultValue(String colType, String defaultValue) {
        if (SqlCode.COL_TYPE_DATETIME.equals(colType)) {
            // 处理时间
            return null;
        } else {
            return "'" + defaultValue + "'";
        }
    }

    private static String changeLength(String colType, String colLength) {
        if (SqlCode.COL_TYPE_DATETIME.equals(colType)) {
            // 字段长度
            return " ";
        } else {
            return "(" + colLength + ")";
        }
    }

    private static void createOracle(SkyTable table, List<SkyTableColumn> tableColumns) {
    }

}
